#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=2e5+5;
inline int read(){
	int x=0,f=1;char ch=getchar();
	while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
	while(isdigit(ch)){x=(x<<1)+(x<<3)+ch-48,ch=getchar();}
	return x*f;
}
int n,ans=1e18;
pair<int,int> p[N];
multiset<int,greater<int> > s1,s2;

signed main(){
	freopen("boast.in","r",stdin);
	freopen("boast.out","w",stdout);
	n=read();
	for(int i=1;i<=n;i++){
		p[i].second=read();
		p[i].first=read();
	}
	sort(p+1,p+1+n);
	for(int i=1;i<=n;i++) s2.insert(p[i].first+p[i].second);
	for(int i=1;i<=n;i++){
		s2.erase(s2.find(p[i].first+p[i].second));

		int del1=0,flg1=0,res=p[i].second;
		if(s1.empty()) del1=(*s2.rbegin()),flg1=2,s2.erase(s2.find(del1));
		else if(s2.empty()) del1=(*s1.rbegin()),flg1=1,s1.erase(s1.find(del1));
		else if((*s2.rbegin())<(*s1.rbegin())+p[i].first)  del1=(*s2.rbegin()),flg1=2,s2.erase(s2.find(del1));
		else del1=(*s1.rbegin()),flg1=1,s1.erase(s1.find(del1));
		res+=del1;
		if(flg1==1) res+=p[i].first;

		int del2=0,flg2=0;
		if(s1.empty()) del2=(*s2.rbegin()),flg2=2,s2.erase(s2.find(del2));
		else if(s2.empty()) del2=(*s1.rbegin()),flg2=1,s1.erase(s1.find(del2));
		else if((*s2.rbegin())<(*s1.rbegin())+p[i].first)  del2=(*s2.rbegin()),flg2=2,s2.erase(s2.find(del2));
		else del2=(*s1.rbegin()),flg2=1,s1.erase(s1.find(del2));
		res+=del2;
		if(flg2==1) res+=p[i].first;

		if(flg1==1) s1.insert(del1);
		else s2.insert(del1);

		if(flg2==1) s1.insert(del2);
		else s2.insert(del2);

		s1.insert(p[i].second);
		ans=min(ans,res);
	}
	printf("%lld",ans);
	return 0;
}